package io.github.wanggit.antrpc.server.telnet.handler.command;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import io.github.wanggit.antrpc.IAntrpcContext;
import io.github.wanggit.antrpc.server.telnet.CmdInfoBean;
import io.github.wanggit.antrpc.server.telnet.handler.CmdDesc;
import org.slf4j.LoggerFactory;

import java.util.Map;

@CmdDesc(
        value = "loglv",
        desc =
                "change class level."
                        + "\r\n\t\t\tExample: loglev io.github.wanggit.antrpc.demo.telnet.api.HelloService info")
public class LogLevelCmd extends AbsCmd {
    public LogLevelCmd(IAntrpcContext antrpcContext, Map<String, CmdInfoBean> telnetCmds) {
        super(antrpcContext, telnetCmds);
    }

    @Override
    protected String intervalDoCmd(String[] arguments) {
        if (arguments.length != 2) {
            return "command format error.";
        }
        String className = arguments[0].trim();
        String logLevel = arguments[1].trim().toUpperCase();
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        Level level = Level.valueOf(logLevel);
        loggerContext.getLogger(className).setLevel(level);
        return "Level set to " + level.toString();
    }
}
